﻿using System;
using System.Text;
using System.Threading.Tasks;
using EasyNetQ;
using EasyNetQ.Topology;

namespace ConsoleAppRabbitmq
{
    class Program
    {


        static void Main(string[] args)
        {

            using (var bus = RabbitHutch.CreateBus("host=172.15.21.122;virtualHost=/;username=wincos;password=q2ZX3s2K09VIpQn3"))
            {
                //订阅
               // bus.PubSub.SubscribeAsync<TextMessage>("DEV", HandleTextMessage);


                var ex = bus.Advanced.ExchangeDeclare("WinCos2BuhlerSmarTExchanges", ExchangeType.Fanout);
                IQueue qu;
                qu = bus.Advanced.QueueDeclare("queue_WincosData_DEV");
                bus.Advanced.Bind(ex, qu, "");
                bus.Advanced.Consume(qu, (body, properties, info) => Task.Factory.StartNew(() =>
                {
                    try
                    {
                        

                            var message = Encoding.UTF8.GetString(body);
                          
                       

                    }
                    catch (Exception e)
                    {

                        throw e;

                    }


                }));

                var input = 0;
                Console.WriteLine("Enter a message. 'Quit' to quit.");
                //发布
                //while ((input < 100))
                //{
                //    input++;
                //    bus.PubSub.Publish<TextMessage>(new TextMessage
                //    {
                //        Text = $"{input}-当前时间：{ DateTime.Now.ToString()}"
                //    });

                //    Console.WriteLine($"第{input}个消息：{ DateTime.Now.ToString()}");
                //}


                Console.ReadLine();
                  
            }


        }
        static void HandleTextMessage(TextMessage textMessage)
        {
            System.Threading.Thread.Sleep(2000);
            Console.WriteLine("Got message: {0}", textMessage.Text);

            t_wincos wincoss = new t_wincos();
            wincoss.Message = textMessage.Text;
        }

    }
     
}
